<?php
class product extends Frontend_Controller {
	public function __construct(){
            parent::__construct();
	}
    
	function index(){
		$cache_html = '';
        $cache_html=$this->load->view('frontend/product/01_list',$this->frontend_data,true);
        if($this->enable_cache)
            $this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
        echo $cache_html;
        //echo $c=$this->load->view('frontend/home/01_home',$this->frontend_data,true);
	}
	function category($type=null,$cate_group_id=0,$cate_id=0,$page=1,$size=20){
		//echo "$type|$cate_group|$cate|$page|$size";
		$cache_html = '';
		$this->frontend_data->active_menu ="menu_$type";
		
		
		if($type===null){
			$this->notice_message('DATA INVALID !');
		}elseif($cate_group_id===0){
			//ALL PRODUCT
			foreach($this->frontend_data->category_types as $c){
				if($type===$c['key']){
					$this->frontend_data->cate_type=$c;
					break;
				}
			}
			$cache_html=$this->load->view('frontend/product/01_list',$this->frontend_data,true);
		}elseif($cate_id===0){
			//PRODUCT BY CATEGORY GROUP
			$filter_cate=array($cate_group_id);
			foreach($this->frontend_data->category_types as $c){
				if($type===$c['key']){
					$this->frontend_data->cate_type=$c;
					break;
				}
			}
			foreach($this->frontend_data->{"category_$type"} as $group_cate){
				if((int)$group_cate->Id===(int)$cate_group_id){
					$this->frontend_data->group_cate=$group_cate;
					if(!empty($group_cate->Children))
					foreach($group_cate->Children as $cate){
						$filter_cate[]=$cate->Id;
						if(!empty($cate->Children))
							$filter_cate = array_merge($filter_cate,array_map(create_function('$o', 'return $o->Id;'), $cate->Children));
					}
					break;
				}
			}
			$this->frontend_data->filter_cate=$filter_cate;
			$cache_html=$this->load->view('frontend/product/01_list_by_group_cate',$this->frontend_data,true);
		}else{
			//PRODUCT BY CATEGORY DETAIL
			$filter_cate=array();
			foreach($this->frontend_data->category_types as $c){
				if($type===$c['key']){
					$this->frontend_data->cate_type=$c;
					break;
				}
			}
			foreach($this->frontend_data->{"category_$type"} as $group_cate){
				if((int)$group_cate->Id===(int)$cate_group_id){
					$this->frontend_data->group_cate=$group_cate;
					if(!empty($group_cate->Children))
					foreach($group_cate->Children as $cate){
						if((int)$cate->Id===(int)$cate_id){
							$this->frontend_data->cate=$cate;
							if(!empty($cate->Children))
								$filter_cate = array_map(create_function('$o', 'return $o->Id;'), $cate->Children);
							$filter_cate[]=$cate_id;
							break;
						}
					}
					break;
				}
			}
			$this->frontend_data->filter_cate=$filter_cate;
			$this->frontend_data->sizes = $this->db
					->query("
                        SELECT
                            size.size_name,
                            size.size_id,
                            count(*) as size_total
                        FROM
                            size
                            LEFT JOIN product ON (
                                    product_status='true'
                                    ".
                                    ((!empty($filter_cate))?"AND product_category_id in(".  implode(',', $filter_cate).")":"")
                                    ."AND CONCAT(',',product_sizes,',') like CONCAT('%,',size_id,',%')
                                )
                        WHERE
                            size.size_category_type = ?
                        GROUP BY size_id
                        ORDER BY size_position ASC, size_insert DESC

                        ",array($type))
					->result();
			$this->frontend_data->colors = $this->db
					->query("
                        SELECT
                            color.color_name,
                            color.color_id,
                            count(*) as color_total
                        FROM
                            color
                            LEFT JOIN product ON (
                                    product_status='true'
                                    ".
                                    ((!empty($filter_cate))?"AND product_category_id in(".  implode(',', $filter_cate).")":"")
                                    ."AND CONCAT(',',product_colors,',') like CONCAT('%,',color_id,',%')
                                )
                        WHERE
                            color.color_category_type = ?
                        GROUP BY color_id
                        ORDER BY color_position ASC, color_insert DESC

                        ",array($type))
					->result();
			$cache_html=$this->load->view('frontend/product/01_list_by_cate',$this->frontend_data,true);
		}
        if($this->enable_cache)
            $this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
        echo $cache_html;
	}
    function search_product($page=1){
        $type = $this->input->post('type');
        $cates = $this->input->post('cates');
        $subcates = $this->input->post('subcates');
        $sizes = $this->input->post('sizes');
        $colors = $this->input->post('colors');
        $perpage = $this->input->post('perpage');
		$sort_column = $this->input->post('sort_column');
		if(!in_array($sort_column,array('insert','price'))) $sort_column = null;
		$sort_action = $this->input->post('sort_action');
		if(!in_array($sort_action,array('asc','desc'))) $sort_action = 'asc';
        if(is_array($subcates) && !empty($subcates)){
            $this->db->where_in('product_category_id',$subcates);
        }elseif(is_array($cates) && !empty($cates)){
            $this->db->where_in('product_category_id',$cates);
        }
        if(is_array($sizes) && !empty($sizes)){
            $strsizewhere = "(";
            foreach ($sizes as $size){
                $size = (int)$size;
                $strsizewhere .= " ( CONCAT(',',product_sizes,',') like '%,$size,%') OR";
            }
            $strsizewhere .= " FALSE)";
            $this->db->where($strsizewhere);
        }
        if(is_array($colors) && !empty($colors)){
            $strsizewhere = "(";
            foreach ($colors as $tmp){
                $tmp = (int)$tmp;
                $strsizewhere .= " ( CONCAT(',',product_colors,',') like '%,$tmp,%') OR";
            }
            $strsizewhere .= " FALSE)";
            $this->db->where($strsizewhere);
        }
		if(!empty($sort_column)){
			$this->db->order_by("product_$sort_column",$sort_action);
		}else{
			$this->db
				->order_by('product_position','asc')
				->order_by('product_insert','desc');
		}
        $this->frontend_data->products = $this->db
                    ->select("
						SQL_CALC_FOUND_ROWS 
                            product.product_id,
                            product.product_title,
                            product.product_title_vi,
                            product.product_alias,
                            product.product_code,
                            product.product_sizes,
                            product.product_colors,
                            product.product_price,
                            product.product_price_vi,
                            product.product_status,
                            product.product_position,
                            product.product_category_id,
                            product.product_total_amount,
                            product.product_amount,
                            product.product_thumb,
                            product.product_tag,
                            product.product_insert,
                            product.product_update,
                            product.product_type
                    ",false)
					->where('product_type',$type)
					->where('product_status','true')
					->limit($perpage,((int)$page-1)*$perpage)
					->get('product')
					->result();
		
        $query = $this->db->query('SELECT FOUND_ROWS() AS `found_rows`;');
        $tmp = $query->row_array();
        $this->frontend_data->total_rows = $total_rows = $tmp['found_rows'];
        $this->frontend_data->page = $page;
        $config['base_url'] = "#/page/";
        $config['total_rows'] = $total_rows;
        $this->frontend_data->perpage = $config['per_page'] = $perpage; 
        //$config['cur_page'] = 2;
        $config['num_links'] = 5;
        $config['use_page_numbers'] = true;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div class="pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';

        $config['first_link'] = '';
        $config['first_tag_open'] = '<li class="first page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '';
        $config['last_tag_open'] = '<li class="last page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = '';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><span>';
        $config['cur_tag_close'] = '</span></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        
        
        $this->pagination->initialize($config);
        $this->frontend_data->html_pageper = $this->pagination->create_links();
        
        $cache_html=$this->load->view('frontend/product/02_search_result',$this->frontend_data,true);
        echo $cache_html;
    }
	
	function productdetail($id=0){
		$cache_html = '';
		$pr = $this->frontend_data->product = $this->db
			->where('product_id',$id)
			->where('product_status','true')
			->get('product')
			->row();
		
		if(!empty($pr)){
			$type = $this->frontend_data->product->product_type;
			foreach($this->frontend_data->category_types as $c){
				if($type===$c['key']){
					$this->frontend_data->cate_type=$c;
					break;
				}
			}
			
			$str_link = "/category/$type/";
			$tmp = $this->get_category_by_id($type,$pr->product_category_id);
			if($tmp){
				foreach($tmp->Parents as $cid){
					if($cid>0){
						$tmp2 = $this->get_category_by_id($type,$cid);
						if($tmp2){
							$str_link .= "$tmp2->Id-$tmp2->Alias/";
							$links[]="<a href=\"$str_link\">$tmp2->Title</a>";
						}else{
							$str_link .= "$cid-unknown/";
							$links[]="<a href=\"$str_link\">unknown</a>";
						}
						
					}
				}
				$links[]="<span class=\"active\">$tmp->Title</span>";
				
			}else{
				$links[]="<span class=\"active\">unknown</span>";
			}
			$pr->link_cates = $links;
			
			$this->frontend_data->sizes = $this->db
					->where('size_category_type',$type)
					->where_in('size_id',explode(',',$pr->product_sizes))
					->order_by('size_position','asc')
					->order_by('size_insert','desc')
					->get('size')
					->result();
			$this->frontend_data->colors = $this->db
					->where('color_category_type',$type)
					->where_in('color_id',explode(',',$pr->product_sizes))
					->order_by('color_position','asc')
					->order_by('color_insert','desc')
					->get('color')
					->result();
		}
		
		
		$cache_html=$this->load->view('frontend/product/04_product_detail',$this->frontend_data,true);
		if($this->enable_cache)
            $this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
        echo $cache_html;
	}
	function related_product($Page=1){
		$CateId = (int)$this->input->post('CateId');
		$Id = (int)$this->input->post('Id');
		$Pagesize = 4;//(int)$this->input->post('Pagesize');
		$type = $this->input->post('Type');
		$tmp = $this->frontend_data->product = $this->db
			->where('product_id',$Id)
			->where('product_status','true')
			->get('product')
			->row();
		if($tmp){
			$this->frontend_data->products = $this->db
                    ->select("
						SQL_CALC_FOUND_ROWS 
                            product.product_id,
                            product.product_title,
                            product.product_title_vi,
                            product.product_alias,
                            product.product_code,
                            product.product_sizes,
                            product.product_colors,
                            product.product_price,
                            product.product_price_vi,
                            product.product_status,
                            product.product_position,
                            product.product_category_id,
                            product.product_total_amount,
                            product.product_amount,
                            product.product_thumb,
                            product.product_tag,
                            product.product_insert,
                            product.product_update,
                            product.product_type
                    ",false)
					->where_in('product_id',explode(',',$tmp->product_related))
					->where('product_id <>',$Id)
					->where('product_status','true')
					->order_by('product_position','asc')
					->order_by('product_insert','desc')
					->limit($Pagesize,((int)$Page-1)*$Pagesize)
					->get('product')
					->result();
			$query = $this->db->query('SELECT FOUND_ROWS() AS `found_rows`;');
			$tmp = $query->row_array();
			$total_rows = $tmp['found_rows'];
			$config['base_url'] = "#/page/";
			$config['total_rows'] = $total_rows;
			$config['per_page'] = $Pagesize; 
			//$config['cur_page'] = 2;
			$config['num_links'] = 5;
			$config['use_page_numbers'] = true;
			$config['uri_segment'] = 3;
			$config['full_tag_open'] = '<div class="pagination"><ul>';
			$config['full_tag_close'] = '</ul></div>';

			$config['first_link'] = '';
			$config['first_tag_open'] = '<li class="first page">';
			$config['first_tag_close'] = '</li>';

			$config['last_link'] = '';
			$config['last_tag_open'] = '<li class="last page">';
			$config['last_tag_close'] = '</li>';

			$config['next_link'] = '';
			$config['next_tag_open'] = '<li class="next page">';
			$config['next_tag_close'] = '</li>';

			$config['prev_link'] = '';
			$config['prev_tag_open'] = '<li class="prev page">';
			$config['prev_tag_close'] = '</li>';

			$config['cur_tag_open'] = '<li class="active"><span>';
			$config['cur_tag_close'] = '</span></li>';

			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			
			
			$this->pagination->initialize($config);
			$this->frontend_data->html_pageper = $this->pagination->create_links();
				
			$cache_html=$this->load->view('frontend/product/06_product_similar',$this->frontend_data,true);
			if($this->enable_cache)
				$this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
			echo $cache_html;
			
		}
		
	}
	
	function similer_product($Page=1){
		$CateId = (int)$this->input->post('CateId');
		$Id = (int)$this->input->post('Id');
		//$Page = (int)$this->input->post('Page');
		//if($Page<1)$Page=1;
		$Pagesize = 4;
		$type = $this->input->post('Type');
		$tmp = $this->get_category_by_id($type,$CateId);
		$tmp = $this->get_category_by_id($type,$tmp->Parent);
		$filter_cate = array_map(create_function('$o', 'return $o->Id;'), $tmp->Children);
		if(!empty($filter_cate)){
			$this->db->where_in('product_category_id',$filter_cate);
		}
		if($tmp){
			$this->frontend_data->products = $this->db
                    ->select("
						SQL_CALC_FOUND_ROWS 
                            product.product_id,
                            product.product_title,
                            product.product_title_vi,
                            product.product_alias,
                            product.product_code,
                            product.product_sizes,
                            product.product_colors,
                            product.product_price,
                            product.product_price_vi,
                            product.product_status,
                            product.product_position,
                            product.product_category_id,
                            product.product_total_amount,
                            product.product_amount,
                            product.product_thumb,
                            product.product_tag,
                            product.product_insert,
                            product.product_update,
                            product.product_type
                    ",false)
					->where('product_id <>',$Id)
					->where('product_type',$type)
					->where('product_status','true')
					->order_by('product_position','asc')
					->order_by('product_insert','desc')
					->limit($Pagesize,((int)$Page-1)*$Pagesize)
					->get('product')
					->result();
		$query = $this->db->query('SELECT FOUND_ROWS() AS `found_rows`;');
        $tmp = $query->row_array();
        $total_rows = $tmp['found_rows'];
		$config['base_url'] = "#/page/";
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $Pagesize; 
        //$config['cur_page'] = 2;
        $config['num_links'] = 5;
        $config['use_page_numbers'] = true;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div class="pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';

        $config['first_link'] = '';
        $config['first_tag_open'] = '<li class="first page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '';
        $config['last_tag_open'] = '<li class="last page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = '';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><span>';
        $config['cur_tag_close'] = '</span></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        
        
        $this->pagination->initialize($config);
        $this->frontend_data->html_pageper = $this->pagination->create_links();
			
			$cache_html=$this->load->view('frontend/product/06_product_similar',$this->frontend_data,true);
			if($this->enable_cache)
				$this->cache->file->save($this->cache_html_name, $cache_html, $this->cache_limit);
			echo $cache_html;
		}
	}
}
?>